package com.erp.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.user.domain.bo.CityBo;
import com.erp.user.domain.bo.ProvinceBo;
import com.erp.user.domain.vo.CityVo;
import com.erp.user.domain.vo.ProvinceVo;
import com.erp.user.service.ICityService;
import com.erp.user.service.IProvinceService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 省份
 *
 * @author erp
 * @date 2023-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/province")
public class ProvinceController extends BaseController {

    private final IProvinceService iProvinceService;
    private final ICityService iCityService;

    /**
     * 查询省份列表
     */
    @SaCheckPermission("system:province:list")
    @GetMapping("/list")
    public TableDataInfo<ProvinceVo> list(ProvinceBo bo, PageQuery pageQuery) {
        return iProvinceService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询城市市列表
     */
    @GetMapping("/getCityList")
    public TableDataInfo<CityVo> getCityList(CityBo bo, PageQuery pageQuery) {
        return iCityService.queryPageList(bo,pageQuery);
    }

    /**
     * 导出省份列表
     */
    @SaCheckPermission("system:province:export")
    @Log(title = "省份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProvinceBo bo, HttpServletResponse response) {
        List<ProvinceVo> list = iProvinceService.queryList(bo);
//        ExcelUtil.exportExcel(list, "省份", ProvinceVo.class, response);
    }

    /**
     * 获取省份详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:province:query")
    @GetMapping("/{id}")
    public R<ProvinceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(iProvinceService.queryById(id));
    }

    /**
     * 新增省份
     */
    @SaCheckPermission("system:province:add")
    @Log(title = "省份", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProvinceBo bo) {
        return toAjax(iProvinceService.insertByBo(bo));
    }

    /**
     * 修改省份
     */
    @SaCheckPermission("system:province:edit")
    @Log(title = "省份", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProvinceBo bo) {
        return toAjax(iProvinceService.updateByBo(bo));
    }

    /**
     * 删除省份
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:province:remove")
    @Log(title = "省份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(iProvinceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
